from selenium import webdriver
from common.base import Base
from selenium.webdriver.common.by import By


class LoginPage(Base):
    user_loc = (By.CLASS_NAME, 'el-input__inner')
    next_loc = (By.XPATH, "//i[@class='userItemImg icon DHCiconfont DHC-tiaozhuan-denglu']")
    pwd_loc = (By.XPATH, "//input[@placeholder='请输入密码']")
    log_button_loc = (By.XPATH, "//i[@class='icon DHCiconfont DHC-tiaozhuan-denglu']")
    welcome_text_loc = (By.XPATH, "//span[@class='userName el-popover__reference']//a")
    plat_sys_loc = (By.TAG_NAME, 'dt')

    def input_user(self, user_text):
        self.sendkeys(self.user_loc, user_text)

    def click_next(self):
        self.click(self.next_loc)

    def input_pwd(self, pwd_text):
        self.sendkeys(self.pwd_loc, pwd_text)

    def click_login_button(self):
        self.click(self.log_button_loc)

    def click_plat_sys(self):
        self.click(self.plat_sys_loc)

    def login(self, username, pwd):
        self.input_user(username)  # 输入用户名
        self.click_next()  # 点击下一步按钮
        self.input_pwd(pwd)  # 输入密码
        self.click_login_button()  # 点击登录按钮
        self.click_plat_sys()  # 点击平台系统按钮

    def get_wel_username(self):
        username = self.gettext(self.welcome_text_loc)
        return username  # 获取登录界面的用户姓名


if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('http://172.28.30.2:9000/platform/#/login')
    driver.maximize_window()
    l = LoginPage(driver)
    l.login('admin', '111111-a')
    text = l.get_wel_username()
    if text == "超级管理员":
        print('登录成功')
    driver.quit()
